package artid.com.arttogoa;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogoa.R;

import artid.com.arttogoa.adapter.ZxAdapter;
import artid.com.arttogoa.adapter.ZxCateAdapter;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.CategoryList;
import artid.com.arttogoa.parser.ConsultationList;
import artid.com.arttogoa.util.AppUtil;
import artid.com.arttogoa.view.FbzxGzDialog;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullToRefreshLayout;
import artid.com.arttogoa.view.pulltorefresh.pullableview.PullableListView;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 
 * 
 * 项目名称：ArtworkArtA 类名称：ArtConsultationActivity 类描述：资讯页面 创建人：耿卫斌 创建时间：2016年5月16日
 * 下午1:20:45
 * 
 */
public class ArtConsultationActivity extends BaseActivity implements
OnClickListener {
	//	private ListView listview;
	private ListView listcate;
	private RelativeLayout type;
	private TextView title_text, title_sqba, info;
	private ZxAdapter mAdapter;
	private ZxCateAdapter adapterCate;
	private Context mContext;
	private FbzxGzDialog myDialog;
	int pagesize = 10;
	String strtype = "";
	private PullableListView mListView;
	private PullToRefreshLayout mPullLayout;
	private boolean isRefreshed = false;// 是否刷新
	private int page=1;
	private List<ConsultationList> list;
	private List<ConsultationList> more_list;
	View back;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_art_zx);
		setContent(this, getIntent());
		mContext = this;
		initData();
	}
	
	private void initData() {
		// TODO Auto-generated method stub
		// rightMenu.setVisibility(View.VISIBLE);
		mContext = this;
		setContent(this, getIntent());
		list=new ArrayList<ConsultationList>();
		more_list=new ArrayList<ConsultationList>();
		//注册当前广播
		IntentFilter filter = new IntentFilter("com.ArtConsultationActivity.receiver");  
		mContext.registerReceiver(broadcastReceiver, filter); 
		mListView =(PullableListView) findViewById(R.id.artconsultation_listivew);
		listcate = (ListView) findViewById(R.id.listView1);
		type = (RelativeLayout) findViewById(R.id.ll_news_type);
		title_sqba = (TextView) findViewById(R.id.title_sqba);
		title_text = (TextView) findViewById(R.id.title_text);
		setBack();
		info = (TextView) findViewById(R.id.tv_info);
		title_text.setOnClickListener(this);
		type.setOnClickListener(this);
		title_sqba.setOnClickListener(this);
		hint = (LinearLayout) findViewById(R.id.ll_hint);
		/**
		 * 判断是否认证，未认证不可申请备案
		 */
		if ("2".equals(AppUtil.getUser().getIs_auth())
				|| "0".equals(AppUtil.getUser().getIs_auth())
				|| "-1".equals(AppUtil.getUser().getIs_auth())) {
			info.setVisibility(View.VISIBLE);
			title_sqba.setVisibility(View.GONE);
		}
		reqtype();// 备案列表
		// reqAtristListNumData();// 艺术品备案状态列表及数量 
		mPullLayout = (PullToRefreshLayout)findViewById(R.id.artconsultation_refresh_view);
		mPullLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				page=1;
				isRefreshed=true;
				reqData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				// TODO Auto-generated method stub
				// pullToRefreshLayout.setB ottomDismiss();
				page++;
				reqData();
			}
		});
	}

	void reqtype() {
		VolleyBasic.getInstance(ArtConsultationActivity.this).post(
				Cache.categoryList, VolleyMap.getInstance().categoryList(),
				CategoryList.class, new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						final List<CategoryList> item = (List<CategoryList>) vd
								.getList();
						if (item.isEmpty()) {
							return;
						}
						strtype = item.get(0).getId();
						adapterCate = new ZxCateAdapter(mContext);
						adapterCate.setdata(item);
						listcate.setAdapter(adapterCate);
						listcate.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
								strtype = item.get(arg2).getId();
								type.setVisibility(View.GONE);
								title_text.setText(item.get(arg2).getTitle());
								Resources res = getResources();
								Drawable myImage = res.getDrawable(R.drawable.jzsx_btn_down);
								title_text.setCompoundDrawablesWithIntrinsicBounds(null, null,
										myImage, null);
								page =1;
								isRefreshed = true;
								reqData();
							}
						});
						reqData();
						// mAdapter = new ZxAdapter(mContext);
						// mAdapter.setdata(item);
						// listview.setAdapter(mAdapter);
					}

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub

					}
				});
	}

	void reqData() {
		VolleyBasic.getInstance(ArtConsultationActivity.this).post(
				Cache.consultationList,
				VolleyMap.getInstance().consultationList(
						String.valueOf(page), String.valueOf(pagesize),
						strtype), ConsultationList.class, new VolleyCallback() {

					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.SUCCEED);
						list=(List<ConsultationList>) vd.getList();
						if (isRefreshed == true) {
							more_list.clear();
							isRefreshed = false;
						}
						if (!list.isEmpty()) {
							more_list.addAll(list);
						}
						if(more_list==null | more_list.size()==0){
							//没数据的时候
							hint.setVisibility(View.VISIBLE);
							return;
						}
						hint.setVisibility(View.GONE);
						if (mAdapter == null) {
							mAdapter=new ZxAdapter(mContext);
							mListView.setAdapter(mAdapter);
						}
						mAdapter.setdata(more_list);
						mAdapter.notifyDataSetChanged();
						mListView.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
								Intent intent = new Intent(
										ArtConsultationActivity.this,
										ArtConsultationDetailActivity.class);
								intent.putExtra("id", more_list.get(arg2).getId());
								startActivity(intent);
							}
						});
					}
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						mPullLayout.loadmoreFinish(mPullLayout.FAIL);
					}
				});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;

		case R.id.title_text:
			if (type.getVisibility() == View.VISIBLE) {
				type.setVisibility(View.GONE);
				Resources res = getResources();
				Drawable myImage = res.getDrawable(R.drawable.jzsx_btn_down);
				title_text.setCompoundDrawablesWithIntrinsicBounds(null, null,
						myImage, null);
			} else {
				type.setVisibility(View.VISIBLE);
				Resources res = getResources();
				Drawable myImage = res.getDrawable(R.drawable.jzsx_btn_up);
				title_text.setCompoundDrawablesWithIntrinsicBounds(null, null,
						myImage, null);
			}
			break;

		case R.id.title_sqba:
			Intent intent = new Intent(ArtConsultationActivity.this,
					ReleaseConsultationActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}

	}
	
	/**
	 * 广播接收器
	 */
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {  

		@Override  
		public void onReceive(Context context, Intent intent) {  
			if(intent.getBooleanExtra("isRefresh", false)==true){
				Log.d("log", "通知资讯页面列表刷新");
				page=1;
				isRefreshed=true;
				mAdapter=null;
				reqData();
			}
		}
	};

	protected void onDestroy() {
		super.onDestroy();
		//销毁广播
		mContext.unregisterReceiver(broadcastReceiver);
	};

}
